//
//  BridgeWKWebViewController.m
//  WebViewTest
//
//  Created by sank zhang on 2018/12/28.
//  Copyright © 2018年 sank zhang. All rights reserved.
//

#import "BridgeWKWebViewController.h"
#import "WebViewJavascriptBridge.h"
#import "MJExtension.h"

@interface BridgeWKWebViewController ()<WKNavigationDelegate,WKUIDelegate>
@property (nonatomic, strong) WebViewJavascriptBridge* bridge;
@property (nonatomic, strong) WKWebView* webView;
@end

@implementation BridgeWKWebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view addSubview:self.webView];
    
    [WebViewJavascriptBridge enableLogging];
    [self loadExamplePage];
    [self registerOCHandler];
    [self callHandler];
}

//注册OC方法，提供给H5调用
- (void)registerOCHandler
{
    //注册一个testObjcCallback方法，给js调用
    //data:js方法传过来的数据
    //responseCallback:回调js
    //bridge.callHandler('testObjcCallback', {'foo': 'bar'}, function(response) {
    //    log('JS got response', response)
    //})
    [self.bridge registerHandler:@"testObjcCallback" handler:^(id data, WVJBResponseCallback responseCallback) {
        NSDictionary *dataDic = [NSJSONSerialization JSONObjectWithData:[data mj_JSONData] options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"----%@",dataDic);
        responseCallback(@"Response from testObjcCallback");
    }];
}

//OC调用js注册的testJavascriptHandler方法
- (void)callHandler
{
    id data = @{ @"greetingFromObjC": @"Hi there, JS!" };
    [self.bridge callHandler:@"testJavascriptHandler" data:data responseCallback:^(id response) {
        NSLog(@"testJavascriptHandler responded: %@", response);
    }];
}

- (void)loadExamplePage
{
    NSString* htmlPath = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html"];
    NSString* appHtml = [NSString stringWithContentsOfFile:htmlPath encoding:NSUTF8StringEncoding error:nil];
    NSURL *baseURL = [NSURL fileURLWithPath:htmlPath];
    [self.webView loadHTMLString:appHtml baseURL:baseURL];
}

//接收到警告面板
- (void)webView:(WKWebView *)webView runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WKFrameInfo *)frame completionHandler:(void (^)(void))completionHandler{
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"哈哈" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        completionHandler();//此处的completionHandler()就是调用JS方法时，`evaluateJavaScript`方法中的completionHandler
    }];
    [alert addAction:action];
    [self presentViewController:alert animated:YES completion:nil];
}

- (WKWebView *)webView
{
    if (_webView == nil) {
        _webView = [[WKWebView alloc] initWithFrame:self.view.frame];
        _webView.navigationDelegate = self;
        _webView.UIDelegate = self;
    }
    return _webView;
}

- (WebViewJavascriptBridge *)bridge
{
    if (_bridge == nil) {
        _bridge = [WebViewJavascriptBridge bridgeForWebView:self.webView];
        [_bridge setWebViewDelegate:self];
    }
    return _bridge;
}

@end
